package df.bean.interfacefile;

import df.bean.db.conn.DBConnection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import df.bean.db.conn.DBConn;
import df.bean.db.table.TRN_Error;
import df.bean.obj.util.JDate;
import df.bean.obj.util.Variables;
/**
 *
 * @author arxim
 */
public class ImportTransactionBean extends InterfaceTextFileBean {
    private ResultSet rs;
    private Statement stm;
    private String transaction_date;
    private String user_id;
    private String hospital_code;
    
    @Override
    public boolean insertData(String fn, DBConnection d) {
        boolean status = true;
        ArrayList<String> a = null;
        String[] sub_data = null;
        String inputMessage = "";
        String temp = "";
        String message = "";
        int insert_count = 0;

        try {
            setConn(d);//connect database
            if(Variables.IS_TEST){ System.out.println("File name : "+fn); }
            setFileName(fn);//set filename read
            copyDataFile();//copy data from file to superclass arraylist member
            a = getData();//copy data from superclass arraylist member
            stm = getConn().getConnection().createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        	this.getConn().setUserID(this.getUserName());
        	this.getConn().setHospitalCode(this.hospital_code);
            rs = stm.executeQuery("SELECT * FROM INT_HIS_BILL WHERE 0<>0");
            for(int i = 0; i<a.size(); i++){
                if(i>0){
                    this.setMessage("File Transaction Error Line : "+i);
                    temp = (String)a.get(i);
                    sub_data = temp.split("[|]");
                    try{
                        rs.moveToInsertRow();
                        message = "Invoice No = "+sub_data[2]+" Invoice Date = "+sub_data[3]
                                  +" Line No = "+sub_data[33].trim()+":"
                                  +new String(sub_data[6].trim().getBytes(),"TIS-620");
                        this.setMessage(message);
                        inputMessage = "Do Update HOSPITAL_CODE";
                        rs.updateString("HOSPITAL_CODE",sub_data[0].trim());//A
                        
                        inputMessage += " EPISODE_TYPE";
                        rs.updateString("EPISODE_TYPE",sub_data[1].trim());//B
                        
                        inputMessage += " BILL_NO";
                        rs.updateString("BILL_NO",sub_data[2].trim());//C
                        inputMessage += " BILL_DATE";
                        rs.updateString("BILL_DATE",sub_data[3].trim());//D
                        inputMessage += " RECEIPT_TYPE_CODE";
                        rs.updateString("RECEIPT_TYPE_CODE",sub_data[4].trim());//E
                        inputMessage += " TRANSACTION_TYPE";
                        if(sub_data[4].trim().equals("AR")){
                            rs.updateString("TRANSACTION_TYPE","INV");
                        }else{
                            rs.updateString("TRANSACTION_TYPE","REV");
                        }
                        inputMessage += " HN_NO";
                        rs.updateString("HN_NO", sub_data[5].trim());//F
                        inputMessage += " PATIENT_NAME";
                        rs.updateString("PATIENT_NAME",new String(sub_data[6].trim().getBytes(),"TIS-620"));//G
                        inputMessage += " EPISODE_NO";
                        rs.updateString("EPISODE_NO",sub_data[7].trim());//H
                        inputMessage += " PAYOR_CODE";
                        rs.updateString("PAYOR_CODE",sub_data[8].trim());//I
                        inputMessage += " PAYOR_NAME";
                        rs.updateString("PAYOR_NAME",new String(sub_data[9].trim().getBytes(),"TIS-620"));//J
                        inputMessage += " PAYOR_CATEGORY_CODE";
                        rs.updateString("PAYOR_CATEGORY_CODE",sub_data[10].trim());//K
                        inputMessage += " PAYOR_CATEGORY_DESC";
                        rs.updateString("PAYOR_CATEGORY_DESC",new String(sub_data[11].trim().getBytes(),"TIS-620"));//L
                        inputMessage += " ADMISSION_TYPE_CODE";                        
                        if(sub_data[12].trim().equals("I")){//M
                            rs.updateString("ADMISSION_TYPE_CODE",sub_data[12].trim());
                        }else{
                            rs.updateString("ADMISSION_TYPE_CODE","O");
                        }
                        inputMessage += " ORDER_ITEM_CODE";                        
                        rs.updateString("ORDER_ITEM_CODE",sub_data[13].trim());//N
                        inputMessage += " ORDER_ITEM_DESCRIPTION";                        
                        rs.updateString("ORDER_ITEM_DESCRIPTION",new String(sub_data[14].trim().getBytes(),"TIS-620"));//O
                        inputMessage += " DOCTOR_PROFILE_CODE";                        
                        rs.updateString("DOCTOR_PROFILE_CODE",sub_data[15].trim());//P
                        inputMessage += " DOCTOR_PROFILE_NAME";                        
                        rs.updateString("DOCTOR_PROFILE_NAME",new String(sub_data[16].trim().getBytes(),"TIS-620"));//Q
                        inputMessage += " DOCTOR_CODE";                        
                        if(sub_data[17].length()<4){//R
                            rs.updateString("DOCTOR_CODE","99999999");
                        }else{
                            rs.updateString("DOCTOR_CODE",sub_data[17].trim());
                        }
                        inputMessage += " DOCTOR_NAME";                        
                        rs.updateString("DOCTOR_NAME",new String(sub_data[18].trim().getBytes(),"TIS-620"));//S
                        inputMessage += " DOCTOR_PRIVATE";                        
                        rs.updateString("DOCTOR_PRIVATE",sub_data[19].trim());//T
                        inputMessage += " AMOUNT_BEF_DISCOUNT";                        
                        rs.updateDouble("AMOUNT_BEF_DISCOUNT",Double.parseDouble(""+sub_data[20].trim()));//U
                        inputMessage += " AMOUNT_OF_DISCOUNT";                        
                        rs.updateDouble("AMOUNT_OF_DISCOUNT",Double.parseDouble(""+sub_data[21].trim()));//V
                        inputMessage += " ORDERED_DATE";                        
                        rs.updateString("ORDERED_DATE",sub_data[22].trim());//W
                        inputMessage += " ORDERED_TIME";                        
                        rs.updateString("ORDERED_TIME",sub_data[23].trim());//X
                        inputMessage += " NATIONALITY_CODE";                        
                        rs.updateString("NATIONALITY_CODE",sub_data[24].trim());//Y
                        inputMessage += " NATIONALITY_DESCRIPTION";                        
                        rs.updateString("NATIONALITY_DESCRIPTION",new String(sub_data[25].trim().getBytes(),"TIS-620"));//Z
                        inputMessage += " FIRST_LOCATION_CODE";                        
                        rs.updateString("FIRST_LOCATION_CODE",sub_data[26].trim());//AA
                        inputMessage += " FIRST_LOCATION_DESC";                        
                        rs.updateString("FIRST_LOCATION_DESC",new String(sub_data[27].trim().getBytes(),"TIS-620"));//AB
                        inputMessage += " FIRST_LOCATION_DEPT_CODE";                        
                        rs.updateString("FIRST_LOCATION_DEPT_CODE",sub_data[28].trim());//AC
                        inputMessage += " FIRST_LOCATION_DEPT_DESC";                        
                        rs.updateString("FIRST_LOCATION_DEPT_DESC",new String(sub_data[29].trim().getBytes(),"TIS-620"));//AD
                        inputMessage += " PATIENT_LOCATION_CODE";                        
                        rs.updateString("PATIENT_LOCATION_CODE",sub_data[30].trim());//AE
                        inputMessage += " PATIENT_LOCATION_DESC";                        
                        rs.updateString("PATIENT_LOCATION_DESC",new String(sub_data[31].trim().getBytes(),"TIS-620"));//AF
                        inputMessage += " PATIENT_LOCATION_DEPT_CODE";                        
                        rs.updateString("PATIENT_LOCATION_DEPT_CODE",sub_data[32].trim());//AG
                        inputMessage += " PATIENT_LOCATION_DEPT_DESC";                        
                        rs.updateString("PATIENT_LOCATION_DEPT_DESC",new String(sub_data[33].trim().getBytes(),"TIS-620"));//AH
                        inputMessage += " RECEIVING_LOCATION_CODE";                        
                        rs.updateString("RECEIVING_LOCATION_CODE",sub_data[34].trim());//AI
                        inputMessage += " RECEIVING_LOCATION_DESC";                        
                        rs.updateString("RECEIVING_LOCATION_DESC",new String(sub_data[35].trim().getBytes(),"TIS-620"));//AJ
                        inputMessage += " RECEIVING_LOCATION_DEPT_CODE";                        
                        rs.updateString("RECEIVING_LOCATION_DEPT_CODE",sub_data[36].trim());//AK
                        inputMessage += " RECEIVING_LOCATION_DEPT_DESC";                        
                        rs.updateString("RECEIVING_LOCATION_DEPT_DESC",new String(sub_data[37].trim().getBytes(),"TIS-620"));//AL
                        inputMessage += " LINE_NO";                        
                        if(sub_data[38].trim().equals("") || sub_data[38].trim().equals("null")){//AM
                            rs.updateString("LINE_NO",(""+JDate.getTimeInMillis()).substring(6)+"/"+i);
                        }else{
                            rs.updateString("LINE_NO",sub_data[38].trim());                            
                        }
                        inputMessage += " VERIFIED_DATE";                        
                        if(sub_data[39].trim().length() < 4){    //  Verified Date
                            rs.updateString("VERIFIED_DATE","");                                                        
                        }else{
                            rs.updateString("VERIFIED_DATE",""+sub_data[39].trim());                            
                        }
                        inputMessage += " VERIFIED_TIME";                        
                        rs.updateString("VERIFIED_TIME",sub_data[40].trim());   // Verified  Time is  41 
                        inputMessage += " BILL_TOTAL_AMOUNT";                        
                        rs.updateString("BILL_TOTAL_AMOUNT",sub_data[41].trim());  // Invoice Total  Amount  is 42
                        inputMessage += " TRANSACTION_DATE";                                                
                        if(this.transaction_date.length()< 2){
                        	rs.updateString("TRANSACTION_DATE",sub_data[3].trim());                        	
                        }else{
                        	rs.updateString("TRANSACTION_DATE",this.transaction_date);
                        }
                        inputMessage += " INVOICE_TYPE";                        
                        if(sub_data[39].trim().equals("")){
                            rs.updateString("INVOICE_TYPE","ORDER");
                        }else{
                            rs.updateString("INVOICE_TYPE","EXECUTE");
                        }
                        rs.insertRow();
                        insert_count++;
                    }catch(Exception e){
                        TRN_Error.writeErrorLog(this.getConn(), "InterfaceTransaction",  this.getMessage(), e.toString(), message,"");
                    }
                }
                
            }
            if(insert_count < a.size()-1){
                setMessage("Error : "+ ""+(a.size()- (insert_count+1))+"/"+(a.size()-1)+" records complete : "+insert_count+" records.");
            }else{
                setMessage("Complete "+(a.size()-1)+"/"+insert_count+" records.");
            }
            setBillDate(sub_data[3].trim());
            status = true;
            this.getConn().commitTrans();
            rs.close();
        } catch (Exception f) {
            setInfo(f.toString());
            status = false;
            this.getConn().rollBackTrans();
        }
        finally {
            try {
                if (rs != null) { 
                    rs.close();
                    rs = null;
                }
                if (stm != null) {
                    stm.close();
                    stm = null;
                }
            }
            catch (Exception ex)  {
                System.out.println(ex);
            }
            
        }
        return status;
    }

    @Override
    public boolean exportData(String fn, String hp, String type, String year, String month, DBConn d, String path) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    public void setDate(String d){
    	this.transaction_date = JDate.saveDate(d);
    }
    public void setUserId(String u){
    	this.user_id = u;
    }
	public void setHospitalCode(String hospitalCode) {
		hospital_code = hospitalCode;
	}

}
